package com.hivekion.system.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.common.core.SystemConstant;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.common.redis.RedisUtil;
import com.hivekion.system.domain.SysStatisticsBiName;
import com.hivekion.system.domain.vo.SysStatisticsBiNameCreateInputVo;
import com.hivekion.system.domain.vo.SysStatisticsBiNameInputVo;
import com.hivekion.system.domain.vo.SysStatisticsBiNameViewVo;
import com.hivekion.system.mapper.SysStatisticsBiNameMapper;
import com.hivekion.system.service.ISysStatisticsBiNameService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.logging.SimpleFormatter;
import java.util.stream.Collectors;

@Service
public class SysStatisticsBiNameServiceImpl extends ServiceImpl<SysStatisticsBiNameMapper, SysStatisticsBiName>
        implements ISysStatisticsBiNameService {

    @Autowired
    private SysStatisticsBiNameMapper biNameMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Boolean checkBiNameNameUnique(String biName, String biNameId) {
        LambdaQueryWrapper<SysStatisticsBiName> StatisticsItemLambdaQueryWrapper =
                Wrappers.lambdaQuery();
        StatisticsItemLambdaQueryWrapper.eq(SysStatisticsBiName::getName, biName);
        if (StringUtils.isNotEmpty(biNameId)) {
            StatisticsItemLambdaQueryWrapper.ne(SysStatisticsBiName::getId, biNameId);
        }
        List<SysStatisticsBiName> list = biNameMapper.selectList(StatisticsItemLambdaQueryWrapper);
        return list.size() > 0;
    }

    @Override
    public String create(SysStatisticsBiNameCreateInputVo inputVo) {
        SysStatisticsBiName item = new SysStatisticsBiName();
        BeanUtils.copyProperties(inputVo, item);
        if (inputVo.getNameType() == null || inputVo.getNameType().equals("")) {
            inputVo.setNameType("report");
        }
        boolean bl = biNameMapper.insert(item) > 0;
        if (bl) this.updateRedis();
        return bl ? item.getId() : null;
    }

    @Override
    public boolean update(SysStatisticsBiNameInputVo inputVo) {
        SysStatisticsBiName biName = getTargetClassic(inputVo.getId());
        BeanUtils.copyProperties(inputVo, biName);
        boolean bl = biNameMapper.updateById(biName) > 0;
        if (bl) this.updateRedis();
        return bl;
    }

    private SysStatisticsBiName getTargetClassic(String biNameId) {
        SysStatisticsBiName biName = biNameMapper.selectById(biNameId);
        if (biName == null) {
            throw new BusinessException(500, "未找到报表名信息");
        }
        return biName;
    }

    @Override
    public boolean delete(String StatisticsItemId) {

        boolean bl = biNameMapper.deleteById(StatisticsItemId) > 0;
        if (bl) this.updateRedis();
        return bl;
    }

    @Override
    public List<SysStatisticsBiNameViewVo> getList() {
        List<SysStatisticsBiName> list = getAllList();
        List<SysStatisticsBiNameViewVo> nodes = new ArrayList<>();
        if (list != null && list.size() > 0) {
            list.forEach(
                    StatisticsItem -> {
                        if (StatisticsItem.getSortCode() == null) {
                            StatisticsItem.setSortCode(0);
                        }
                        SysStatisticsBiNameViewVo vo = new SysStatisticsBiNameViewVo();
                        BeanUtils.copyProperties(StatisticsItem, vo);
                        if (StatisticsItem.getIsLoop() != null && StatisticsItem.getIsLoop().equals("是")) {
                            vo.setReportingDeadline("截止日期：" + StatisticsItem.getEndTime().substring(5));
                        }
                        if (StatisticsItem.getIsLoop() != null && StatisticsItem.getIsLoop().equals("否")) {
                            vo.setReportingDeadline("开始时间:" + StatisticsItem.getStartTime() + " 截止日期：" + StatisticsItem.getEndTime());
                        }
                        nodes.add(vo);

                    });
        }
        return nodes.stream()
                .sorted(Comparator.comparing(SysStatisticsBiNameViewVo::getSortCode))
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println("2020-01-01".substring(5));
    }

    @Override
    public SysStatisticsBiName getInfo(String nameId) {
        SysStatisticsBiName biName = biNameMapper.selectById(nameId);
        if (biName == null) {
            throw new BusinessException(500, "未找到报表名信息");
        }
        return biName;
    }

    @Override
    public SysStatisticsBiNameViewVo getBiNameInfo(String targetClassicName) {

        // TODO 通过关键词检索
        return null;
    }

    /*
     * redis 缓存数据
     * */
    public List<SysStatisticsBiName> getAllList() {
        if (redisUtil.hasKey(SystemConstant.redis_biname_data)) {
            List<SysStatisticsBiName> list =
                    JSON.parseArray(
                            redisUtil.get(SystemConstant.redis_biname_data).toString(),
                            SysStatisticsBiName.class);
            return list;
        } else {
            List<SysStatisticsBiName> allBiName = biNameMapper.selectList(null);
            redisUtil.set(SystemConstant.redis_biname_data, JSON.toJSONString(allBiName));
            return allBiName;
        }
    }

    private void updateRedis() {
        List<SysStatisticsBiName> allBiName = biNameMapper.selectList(null);
        redisUtil.set(SystemConstant.redis_biname_data, JSON.toJSONString(allBiName));
    }
}
